package com.letv.android.client.view;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.TextView;

import com.letv.android.young.client.R;
import com.letv.android.client.activity.AlbumPlayActivity;
import com.letv.android.client.activity.AlbumPlayRoomActivity;
import com.letv.core.utils.LogInfo;
import com.letv.core.utils.StatisticsUtils;
import com.letv.core.utils.TipUtils;
import com.letv.core.utils.UIsUtils;
import com.letv.datastatistics.util.PageIdConstant;
import com.ledim.utils.ToastUtil;

/**
 * 下载的情况下卡顿弹出框
 * 
 * @author zhuqiao
 *
 */
public class BlockDialog extends Dialog {
    private Context mContext;
    private BlockDialogCallback mCallback;

    public interface BlockDialogCallback {

        void clickButton(boolean pause);
    }

    public BlockDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
        initView();
    }

    public BlockDialog(Context context, int theme) {
        super(context, theme);
        this.mContext = context;
        initView();
    }

    public BlockDialog(Context context) {
        super(context, R.style.letv_custom_dialog);
        this.mContext = context;
        initView();
    }

    public void setCallback(BlockDialogCallback callback) {
        mCallback = callback;
    }

    private void initView() {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        View root = View.inflate(getContext(), R.layout.letv_block_dialog_layout, null);
        this.setContentView(root, new LayoutParams(UIsUtils.dipToPx(mContext,270), LayoutParams.WRAP_CONTENT));
        TextView downLoadContinue = (TextView) root.findViewById(R.id.download_continue);
        TextView downLoadPause = (TextView) root.findViewById(R.id.download_pause);
        TextView messageTextView = (TextView) root.findViewById(R.id.textView_message);
        messageTextView.setText(TipUtils.getTipMessage(mContext,"100005", R.string.block_dialog_message));
        MyOnClickListener listener = new MyOnClickListener();
        downLoadContinue.setOnClickListener(listener);
        downLoadPause.setOnClickListener(listener);
    }

    class MyOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.download_continue) {
                // do nothing
                // 5.7
                LogInfo.LogStatistics("暂停缓存弹出框-继续缓冲");
                StatisticsUtils.staticticsInfoPost(mContext, "0", "c687", null, 2, null,
                        UIsUtils.isLandscape(mContext) ? PageIdConstant.fullPlayPage : PageIdConstant.halpPlayPage,
                        null, null, null, null, null);
                if (mCallback != null) {
                    mCallback.clickButton(false);
                }
            } else if (v.getId() == R.id.download_pause) {
                // 5.7
                LogInfo.LogStatistics("暂停缓存弹出框-暂停缓冲");
                StatisticsUtils.staticticsInfoPost(mContext, "0", "c687", null, 1, null,
                        UIsUtils.isLandscape(mContext) ? PageIdConstant.fullPlayPage : PageIdConstant.halpPlayPage,
                        null, null, null, null, null);
//                DownloadManager.pauseAllDownload();

                // fix bug 【Gphone V5.7】播放卡顿时，弹出是否暂停缓存的对话窗，点击继续缓存或暂停缓存按钮，没有出现toast提示
                ToastUtil.toast("已暂停全部缓存");
                if (mCallback != null) {
                    mCallback.clickButton(true);
                }
            }
            if(mContext instanceof AlbumPlayActivity) {
                AlbumPlayActivity.sIsBlockPause = false;
            } else if(mContext instanceof AlbumPlayRoomActivity) {
                AlbumPlayRoomActivity.sIsBlockPause = false;
            }

            BlockDialog.this.dismiss();
        }

    }

}
